<?php

/**
 * 文件注释
 *
 * Author: Liam
 * Datetime: 11/26/17 00:00 PM
 */

namespace ApiBundle\Controller;

use BaseBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;

class ArticleController extends BaseController
{
    //  获取所有分类
    public function classifyAllAction()
    {
        $conn = $this->get('database_connection');

        $data = $conn->fetchAll("SELECT * FROM article_classify");

        return $this->jsonResponse('操作成功', 0, $data);
    }

    // article列表
    public function articleListAction(Request $request)
    {
        $id = $request->get("id","1");
        $conn = $this->get('database_connection');

        $page = $request->get("page","1");

        $rows = $request->get("rows","5");

        $sql = "SELECT * FROM article WHERE article_classify_id = '". $id ."'";

        //搜索条件下的总数
        $search_all_count = count($conn->fetchAll($sql));

        $all_page = ceil($search_all_count / $rows);

        // 分页
        $m = ($page - 1) * $rows;

        $sql .= " LIMIT $m,$rows";


        $data = $conn->fetchAll($sql);
//        $list = $conn->fetchAll("SELECT * FROM article WHERE article_classify_id = '". $id ."'");
//        $data['list'] = $list;

        return $this->jsonResponse('操作成功', 0, $data);
    }

    /**
     * article操作 （新增或修改）
     *
     * $id 为null时 => 新增，反之为编辑
     *
     */
    public function articleOperaAction(Request $request)
    {
        $conn = $this->get('database_connection');

        $id = $request->get('id');
        $title = $request->get('title');
        $classify_id = $request->get('classify_id');
        $content = $request->get('content');
        $classify_array = $conn->fetchAssoc("SELECT * from article_classify WHERE id = '". $classify_id ."'");

        $data = array(
          'title' => $title,
          'article_classify_id' => $classify_id,
          'article_classify_name' => $classify_array['name'],
          'content' => $content
        );

        if ($id === null) {
            $data['created_at'] = date('y-m-d h:i:s',time());
            $conn->insert('article' , $data);
        } else {
            $data['updated_at'] = date('y-m-d h:i:s',time());
            $conn->update('article', $data, array('id'=> $id));
        }
        return $this->jsonResponse('操作成功', 0);
    }

    /**
     * 删除文章
     *
     */
    public function articleDelAction(Request $request)
    {
        $id = $request->get('id');
        if ($id === null) {
            return $this->jsonResponse('缺少参数', 1);
        }

        $conn = $this->get('database_connection');
        $where = array(
            'id' => $id
        );

        $conn->delete('article', $where);
        return $this->jsonResponse('操作成功', 0);
    }

    /**
     * 获取文章详情
     *
     */
    public function articleInfoAction(Request $request)
    {
        $token = $request->get('token');
        $id = $request->get('id');

        $conn = $this->get('database_connection');
        $user = $conn->fetchAssoc("SELECT * FROM user WHERE token = '". $token ."'");

        if ($token == $user['token']) {
            $info = $conn->fetchAssoc("SELECT * FROM article WHERE id = '". $id ."'");
            return $this->jsonResponse('操作成功', 0, $info);
        } else {
            return $this->jsonResponse('登录失效，请重新登录', 5);
        }
    }
}